package com.group02.community.controller;



import com.baomidou.mybatisplus.core.metadata.IPage;
import com.group02.community.annotation.Log;
import com.group02.community.enmus.BusinessType;
import com.group02.community.entity.dict.SysDictType;
import com.group02.community.service.SysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author xl
 * @since 2022-07-01
 */
@Api
@RestController
@RequestMapping("/intelligence_community/sys-dict-type")
@CrossOrigin
public class SysDictTypeController {


    @Autowired
    private SysDictTypeService sysDictTypeService;

    @PostMapping("dictionaries/page")
    @ApiOperation("字典类型分页")
    public ResponseEntity<IPage<SysDictType>> selectAllDictionaries(Long current, @RequestBody SysDictType sysDictType){
        IPage<SysDictType> dictTypeIPage = sysDictTypeService.selectAllDictionaries(current,10L,sysDictType);
        return ResponseEntity.ok(dictTypeIPage);
    }

    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping("dictionary")
    @ApiOperation("字典类型新增")
    public ResponseEntity<String> addDictionaries(SysDictType sysDictType){
        sysDictTypeService.save(sysDictType);
        return ResponseEntity.ok("新增成功");
    }

    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping("dictionary")
    @ApiOperation("字典类型修改")
    public ResponseEntity<String> updateDictionaries(@RequestBody SysDictType sysDictType) throws IOException {
        Long[] longs = {sysDictType.getDictId()};
        if (!sysDictTypeService.hasData(longs)){
            return ResponseEntity.ok("该类型下面有数据不能删除");
        }else {
            sysDictTypeService.updateById(sysDictType);
            return ResponseEntity.ok("修改成功");
        }
    }

//    @DeleteMapping("dictionary/{id}")
//    @ApiOperation("字典类型删除")
//    public ResponseEntity<String> deleteDictionaries(@PathVariable Long id){
//        if (sysDictTypeService.hasData(id)){
//            return ResponseEntity.ok("该类型下面有数据不能删除");
//        }else {
//            sysDictTypeService.removeById(id);
//            return ResponseEntity.ok("删除成功");
//        }
//    }

    @DeleteMapping("dictionaries/{id}")
    @ApiOperation("字典类型批量删除")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    public ResponseEntity<String> deleteSelectDictionaries(@PathVariable Long[] id){
        if (!sysDictTypeService.hasData(id)){
            return ResponseEntity.ok("该类型下面有数据不能删除");
        }else {
            sysDictTypeService.deleteSelectDictionaries(id);
            return ResponseEntity.ok("删除成功");
        }
    }

    @GetMapping("downloadExcel")
    @ApiOperation("导出数据")
    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    public ResponseEntity<String> downloadExcel(HttpServletResponse response) throws IOException {
        sysDictTypeService.downloadExcel(response);
        return ResponseEntity.ok("导出成功");
    }

}

